﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Criterion;

namespace FantastPlatform.Framework.Core
{
    /// <summary>
    /// 查询条件
    /// </summary>
    public class Condition
    {
        /// <summary>
        /// 查询字段
        /// </summary>
        public string Field { get; set; }
        /// <summary>
        /// 字段类型
        /// </summary>
        public string Type { get; set; }
        /// <summary>
        /// 查询运算符
        /// </summary>
        Comparisons comparison;
        public Comparisons Comparison
        {
            get
            {
                return comparison;
            }
            set
            {
                comparison = value;
            }
        }
        private object value1;
        /// <summary>
        /// 查询值
        /// </summary>
        public object Value1
        {
            get
            {
                return this.value1;
            }
            set
            {
                if (this.Type.Equals(typeof(DateTime).FullName))
                {
                    if (value != null)
                    {
                        this.value1 = DateTime.Parse(value.ToString());
                    }
                }
                else if (this.Type.Equals(typeof(bool).FullName))
                {
                    if (value != null)
                    {
                        this.value1 = bool.Parse(value.ToString());
                    }
                }
                else if (this.Type.Equals(typeof(string).FullName))
                {
                    if (this.comparison.Equals(Comparisons.Like))
                    {
                        this.value1 = "%" + value.ToString() + "%";
                    }
                    else
                    {
                        this.value1 = value.ToString();

                    }
                }
                else
                {
                    int s;
                    if (int.TryParse(value.ToString(), out s))
                    {
                        this.value1 = s;
                    }
                }
            }
        }

        private object value2;
        /// <summary>
        /// 查询值2（用于between）
        /// </summary>
        public object Value2
        {
            get
            {
                return value2;
            }
            set
            {
                if (this.Type.Equals(typeof(DateTime).FullName))
                {
                    if (value != null)
                    {
                        this.value2 = DateTime.Parse(value.ToString());
                    }
                }
            }
        }
        private Relations relation = Relations.Null;
        /// <summary>
        /// 与上一个查询条件的关系
        /// </summary>
        public Relations Relation
        {
            get
            {
                return relation;
            }
            set
            {
                relation = value;
            }
        }
    }
    /// <summary>
    /// 查询组(一个查询组由多个平级的查询组成，如（a=b and c=d or e=f）为一个查询组
    /// （(a=b or c=d) and e=f）则为两个查询组
    /// </summary>
    public class ConditionGroup
    {
        public string Id
        {
            get;
            set;
        }
        private Relations relation = Relations.Null;
        /// <summary>
        /// 与上一个查询组的关系
        /// </summary>
        public Relations Relation
        {
            get
            {
                return relation;
            }
            set
            {
                relation = value;
            }
        }
        /// <summary>
        /// 查询组的子集
        /// </summary>
        private IList<ConditionGroup> conditionGroup = new List<ConditionGroup>();
        public IList<ConditionGroup> Children
        {
            get
            {
                return conditionGroup;
            }
            set
            {
                conditionGroup = value;
            }
        }
        private IList<Condition> conditions = new List<Condition>();
        public IList<Condition> Conditions
        {
            get
            {
                return conditions;
            }
            set
            {
                conditions = value;
            }
        }
    }
    //关系符
    public enum Relations
    {
        /// <summary>
        /// 并且
        /// </summary>
        And,
        /// <summary>
        /// 或者
        /// </summary>
        Or, 
        Null
    }
    //运算符
    public enum Comparisons
    {
        /// <summary>
        /// 等于
        /// </summary>
        Eq,
        /// <summary>
        ///大于 
        /// </summary>
        Gt,
        /// <summary>
        /// 是否等于集合中的元素
        /// </summary>
        In,
        /// <summary>
        /// 小于
        /// </summary>
        Lt,
        /// <summary>
        /// 之间
        /// </summary>
        Between,
        /// <summary>
        /// 模糊查询
        /// </summary>
        Like,
        /// <summary>
        /// 不等于
        /// </summary>
        NotEq
    }
}
